package com.jeesite.modules.goods.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.modules.goods.entity.ZtFinancial;
import com.jeesite.modules.goods.entity.ZtGoodsOrder;
import com.jeesite.modules.goods.entity.ZtQuan;
import com.jeesite.modules.goods.entity.ZtWl;
import com.jeesite.modules.goods.service.ZtFinancialService;
import com.jeesite.modules.goods.service.ZtGoodsOrderService;
import com.jeesite.modules.jdProduct.entity.ZtAfterSale;
import com.jeesite.modules.jdProduct.service.ZtAfterSaleService;
import com.jeesite.modules.jdProduct.utils.AfsServiceEnum;
import com.jeesite.modules.recovery.entity.ZtWL;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;
import com.jeesite.modules.xcxAdmin.service.ZtAdminService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.jeesite.common.web.BaseController.text;
import static com.jeesite.common.web.http.ServletUtils.renderResult;

@Controller
@RequestMapping(value = "${adminPath}/ztAfterSale")
public class ZtAfterSaleController {

    @Autowired
    private ZtAfterSaleService ztAfterSaleService;
    @Autowired
    private ZtAdminService ztAdminService;

    @Autowired
    private ZtGoodsOrderService ztGoodsOrderService;

    @Autowired
    private ZtFinancialService ztFinancialService;
    @RequiresPermissions("ztAfterSale:ztAfterSale:view")
    @RequestMapping(value = {"list",""})
    public String list(ZtAfterSale ztAfterSale){

        return "modules/ztAfterSale/ztAfterSaleList";
    }

    @RequiresPermissions("ztAfterSale:ztAfterSale:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<ZtAfterSale> listData(ZtAfterSale ztAfterSale, HttpServletRequest request,
                       HttpServletResponse response){
        Page<ZtAfterSale> page = ztAfterSaleService.findPage(new Page<ZtAfterSale>(request,response),ztAfterSale);
        return page;
    }

    @RequiresPermissions("ztAfterSale:ztAfterSale:view")
    @RequestMapping(value = "form")
    public String form(ZtAfterSale ztAfterSale, HttpServletRequest request,
                       HttpServletResponse response, Model model){

        List<Map<String,String>> ll= ZtWL.toList();
        List<ZtWl> logisticsList=new ArrayList<ZtWl>();
        for(Map ma:ll){
            ZtWl ztwl=new ZtWl();
            ztwl.setLogisticsCode(ma.get("logisticsCode").toString());
            ztwl.setLogisticsName(ma.get("logisticsName").toString());
            logisticsList.add(ztwl);
        }
        ztAfterSale = ztAfterSaleService.get(ztAfterSale);
        List<ZtQuan> ztQuanselfList = new ArrayList<ZtQuan>();

        model.addAttribute("logisticsList",logisticsList);
        model.addAttribute("ztAfterSale", ztAfterSale);
        return "modules/ztAfterSale/ztAfterSaleForm";
    }

    /**
     * 保存数据  /wewoo/userfiles/fileupload/
     */
    @RequiresPermissions("ztAfterSale:ztAfterSale:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated ZtAfterSale ztAfterSale) {

        if(ztAfterSale.getAfsServiceStep()!=null){
            if(ztAfterSale.getAfsServiceStep()==50){
                ZtGoodsOrder ztGoodsOrder = new ZtGoodsOrder();
                ztGoodsOrder.setId(ztAfterSale.getOrder());
                ztGoodsOrder = ztGoodsOrderService.get(ztGoodsOrder);
                ztGoodsOrder.setSalestatus("5");
                if(ztAfterSale.getType()==10){
                    ztGoodsOrder.setSalestatus("3");
                    ztGoodsOrder.setOrderstatus("4");
                    ZtAdmin admin = ztAdminService.get(ztAfterSale.getAdminId());
                    Double balance = ztGoodsOrder.getBalance()-ztGoodsOrder.getFreight();
                    Double totalPrice = ztGoodsOrder.getTotalPrice()+balance;
                    admin.setCashAmount(admin.getCashAmount() + totalPrice);
                    admin.setAmount(admin.getAmount() - balance);
                    if(balance<0D){
                        ztGoodsOrder.setTotalPrice(totalPrice);
                    }
                    ztGoodsOrder.setBalance(balance);
                    forBalance(ztGoodsOrder);
                    ztAdminService.update(admin);
                }
                ztGoodsOrderService.update(ztGoodsOrder);
            }
            AfsServiceEnum afsServiceEnumByValue = AfsServiceEnum.getAfsServiceEnumByValue(ztAfterSale.getAfsServiceStep());
            ztAfterSale.setAfsServiceStepName(afsServiceEnumByValue.getName());
        }

        ztAfterSaleService.update(ztAfterSale);
        return renderResult(Global.TRUE, text("保存售后成功！"));
    }

    public void forBalance(ZtGoodsOrder ztGoodsOrder){
        double balance=ztGoodsOrder.getBalance();
        double total = ztGoodsOrder.getTotalPrice();
        List<ZtFinancial> list = new ArrayList<>();

        if(balance>0D){
            ZtFinancial ztFinancial=new ZtFinancial();
            ztFinancial.setAudit("2");
            ztFinancial.setType("5");
            ztFinancial.setContent("积分支付退款-"+ztGoodsOrder.getZtGoods().getTitle());
            ztFinancial.setZtAdmin(ztGoodsOrder.getZtAdmin());
            ztFinancial.setPricestr("+￥"+String.format("%.2f",balance));
            ztFinancial.setPrice(balance);
            ztFinancial.setCreateDate(new Date());
            ztFinancial.setUsername(ztGoodsOrder.getZtAdmin().getUsername());
            ztFinancial.setHeadimgurl(ztGoodsOrder.getZtAdmin().getHeadimgurl());
            ztFinancial.setPhone(ztGoodsOrder.getZtAdmin().getPhone());
            ztFinancial.setZtGoods(ztGoodsOrder.getZtGoods());
            ztFinancial.setStatus("0");
            ztFinancial.setOperation("系统");
            list.add(ztFinancial);
        }
        if(total>0D){

            ZtFinancial ztFinancial=new ZtFinancial();
            ztFinancial.setAudit("2");
            ztFinancial.setType("5");
            ztFinancial.setContent("微信支付退款-"+ztGoodsOrder.getZtGoods().getTitle());
            ztFinancial.setZtAdmin(ztGoodsOrder.getZtAdmin());
            ztFinancial.setPricestr("+￥"+String.format("%.2f",total));
            ztFinancial.setPrice(total);
            ztFinancial.setCreateDate(new Date());
            ztFinancial.setUsername(ztGoodsOrder.getZtAdmin().getUsername());
            ztFinancial.setHeadimgurl(ztGoodsOrder.getZtAdmin().getHeadimgurl());
            ztFinancial.setPhone(ztGoodsOrder.getZtAdmin().getPhone());
            ztFinancial.setZtGoods(ztGoodsOrder.getZtGoods());
            ztFinancial.setStatus("0");
            ztFinancial.setOperation("系统");
            list.add(ztFinancial);
        }
        if(list.size()>0){
            ztFinancialService.insertBatch(list);
        }
    }
}
